package com.association.main.frontcontroller;

import com.association.main.common.R;
import com.association.main.domain.ApplyJoin;
import com.association.main.domain.Clubs;
import com.association.main.domain.Members;
import com.association.main.dto.ApplyJoinDto;
import com.association.main.service.ApplyJoinService;
import com.association.main.service.ClubsService;
import com.association.main.service.MembersService;
import com.association.main.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/front")
public class MyApplyController {
    @Autowired
    private ApplyJoinService applyJoinService;

    @Autowired
    private MembersService membersService;

    @Autowired
    private ClubsService clubsService;

    @GetMapping("/apply/list")
    public R<Page> myApply(int page,HttpServletRequest request){
        Page pageInfo=new Page(page,10);
        String token = request.getHeader("token");
        Integer userId = JwtUtil.getUserId(token);
        //获取用户ID
        LambdaQueryWrapper<ApplyJoin> applyJoinLambdaQueryWrapper=new LambdaQueryWrapper<>();
        applyJoinLambdaQueryWrapper.eq(ApplyJoin::getMemberId,userId);
        applyJoinLambdaQueryWrapper.select(
                ApplyJoin::getId,
                ApplyJoin::getClubId,
                ApplyJoin::getMsg,
                ApplyJoin::getText,
                ApplyJoin::getStatus
        );
        List<ApplyJoin> list = applyJoinService.list(applyJoinLambdaQueryWrapper);
        applyJoinService.page(pageInfo,applyJoinLambdaQueryWrapper);
        List<ApplyJoinDto> applyJoinDtos=list.stream().map((item)->{
            Clubs club = clubsService.getById(item.getClubId());
            ApplyJoinDto applyJoinDto=new ApplyJoinDto(club.getName(),club.getLogo());
            BeanUtils.copyProperties(item,applyJoinDto);
            return applyJoinDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(applyJoinDtos);
        return R.success(pageInfo);
    }

    @GetMapping("/apply/myapply")
    public R<ApplyJoinDto> getMyApplyById(int id, HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = JwtUtil.getUserId(token);
        LambdaQueryWrapper<ApplyJoin> applyJoinLambdaQueryWrapper=new LambdaQueryWrapper<>();
        applyJoinLambdaQueryWrapper.eq(ApplyJoin::getMemberId,userId).eq(ApplyJoin::getId, id);
        applyJoinLambdaQueryWrapper.select(
                ApplyJoin::getId,
                ApplyJoin::getClubId,
                ApplyJoin::getMsg,
                ApplyJoin::getText,
                ApplyJoin::getStatus,
                ApplyJoin::getCreateTime
        );
        ApplyJoin applyJoin = applyJoinService.getOne(applyJoinLambdaQueryWrapper);
        Clubs club = clubsService.getById(applyJoin.getClubId());
        ApplyJoinDto applyJoinDto=new ApplyJoinDto(club.getName(),club.getLogo());
        BeanUtils.copyProperties(applyJoin, applyJoinDto);
        return R.success(applyJoinDto);
    }


}
